let envVersion = ''
// #ifndef H5
let AccountInfo = uni.getAccountInfoSync()
let miniProgram = AccountInfo.miniProgram
envVersion = miniProgram.envVersion
// #endif
// #ifdef H5
envVersion = process.env.NODE_ENV
// #endif
const baseUrl = (envVersion === 'development' || envVersion == 'develop' || envVersion == 'trial')  ? 'https://indoor.ipark.vip/api' : 'https://gate.ipark.vip/api';
import Vue from 'vue'
let tokenFlag = false
const request = params => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中...',
		});
		const header = params.header || {
			'content-type': 'application/json;charset=UTF-8',
			'Authentication': uni.getStorageSync('token') || ''
		}
		uni.request({
			method: params.method,
			url: baseUrl + params.url,
			data: params.data,
			header: header,
			dataType: 'json',
		}).then(async (response) => {
			uni.hideLoading()
			let {
				statusCode,
				data
			} = response;
			let {
				resultCode,
				resultMsg
			} = data
			//token失效
			if (resultCode === "100000" || resultCode === "100001") {
				if(tokenFlag) return
				tokenFlag = true
				uni.removeStorageSync('token');
				let vm = new Vue()
				await vm.$store.dispatch('user/setTokenUserInfo',vm)
				tokenFlag = false
			}else if(resultCode === "100049") {
				// // #ifdef MP-ALIPAY
				// my.showToast({
				// 	content: `请求出错: ${resultMsg}`,
				// 	type: 'none',
				// 	// mask: true,
				// 	duration: 6000
				// })
				// // #endif
				// // #ifndef MP-ALIPAY
				// uni.showToast({
				// 	title: `请求出错: ${resultMsg}`,
				// 	icon: 'none',
				// 	mask: true,
				// 	duration: 1000 * 60 * 60 * 2
				// })
				// // #endif
				uni.showModal({
					title: '提示',
					content: resultMsg,
					success: function (res) {
						if (res.confirm) {
							// console.log('用户点击确定');
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			}else if (resultCode !== "0") {
				uni.showToast({
					title: `请求出错: ${resultMsg}`,
					icon: 'none'
				})
			}else if (statusCode !== 200) {
				uni.showToast({
					title: `请求出错: ${statusCode}`,
					icon: 'none'
				})
			}else{
				resolve(data);
			}
		}).catch(error => {
			uni.hideLoading()
			reject(error)
		})
	});
}
export default request